package com.hyh.fireworks.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyh.fireworks.entity.Menu;
import com.hyh.fireworks.entity.Posts;
import com.hyh.fireworks.entity.User;
import com.hyh.fireworks.mapper.MenuMapper;
import com.hyh.fireworks.mapper.PostsMapper;
import com.hyh.fireworks.mapper.UserMapper;
import com.hyh.fireworks.service.IHomeService;
import com.hyh.fireworks.vo.HomeVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Summerday
 */
@Service
public class IHomeServiceImpl implements IHomeService {

    @Resource
    MenuMapper menuMapper;

    @Resource
    UserMapper userMapper;

    @Resource
    PostsMapper postsMapper;

    @Override
    public HomeVo content() {
        HomeVo homeVo = new HomeVo();
        User user = userMapper.selectById(1);
        List<Menu> menus = menuMapper.selectList(
                new QueryWrapper<Menu>().orderByDesc("priority"));
        List<Posts> postsList = getPostsList(0, 4);
        homeVo.setMenus(menus);
        homeVo.setUser(user);
        homeVo.setPostsList(postsList);
        return homeVo;
    }

    @Override
    public List<Posts> getPostsList(Integer current, Integer pageSize) {
        QueryWrapper<Posts> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("is_top")
                .orderByDesc("top_priority")
                .orderByDesc("published_time")
                .eq("status", 1);

        Page<Posts> postsPage = postsMapper.selectPage(new Page<>(current, pageSize), wrapper);
        return postsPage.getRecords();
    }
}
